class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
        unordered_map<int,vector<int> > hash;
        vector<vector<int> > ret;
        int n = groupSizes.size();
        
        for(int i = 0;i < n;i++){
            int num = groupSizes[i];
            hash[num].push_back(i);
        }
        
        for(auto& [k,v] : hash){
            if(v.size() == k){
                ret.push_back(v);
            }else{
                int cnt = v.size() / k;
                int start = 0;
                for(int i = 0;i < cnt;i++){
                    ret.push_back(vector<int>(v.begin()+start,v.begin()+start+k));
                    start += k;
                }
            }
        }
        return ret;
    }
};
